<?php

// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();

jimport( 'joomla.html.parameter' );

class JCKHelper
{

	function & getTable( $name, $prefix = 'JCKTable', $config = array())
	{
				
		$path = JPATH_COMPONENT.DS.'tables';
		JTable::addIncludePath($path);

		// Clean the name
		$prefix = preg_replace( '/[^A-Z0-9_]/i', '', $prefix );

		//Make sure we are returning a DBO object
		if (!array_key_exists('dbo', $config))  {
			$config['dbo'] = & JFactory::getDBO();
		}

		$instance =& JTable::getInstance($name, $prefix, $config );
		return $instance;
	}

	 function & geTtoolbarParams($editor,$args = array())
	 {
		 
		 if( count($args) > 1) 
		{
			$row = $args[1];		
		}
		
		        
	   	if(is_a($args[0] ,'JParameter'))
		{
			$params = $args[0];
		}
		else
		{   	if( $row) 
			{
			    $params = new JParameter($row->params);		
			}
			else
			{
				$row = & JCKHelper::getTable('toolbar');
				// load the row from the db table
				$row->load( $args[0]);
				//get toolbar parameter
				$params = new JParameter($row->params);
			}
		}
		
		$editor_params   = new JParameter($editor->params);
		$toolbar = $params->get('toolbar',$row->name);
		$skins = $params->get('skin', $editor_params->def( 'skin','office2003'));
		$width = $params->get('wwidth', $editor_params->def( 'wwidth','100%'));
		
	
		$editor_params->set( 'toolbar',$toolbar);
		$editor_params->set( 'skin', $skins );
		$editor_params->set( 'wwidth', $width);
	    $editor_params->Set( 'hheight',300);			
		return $editor_params;
	 }
	
	
	
	function & getEditorPluginConfig($namspace = 'config')
	{
		$path = JPATH_PLUGINS.DS.'editors'.DS.'jckeditor' .DS.'includes'.DS.'ckeditor';
		
		require_once($path.DS.'plugins.php');
		require_once($path.DS.'plugins'.DS.'toolbarplugins.php');
		
		$config = new JRegistry($namspace);
		
		$pluginConfig = new JCKToolbarPlugins();
			
		$config->loadObject($pluginConfig);						
		
	  	return 	$config;	
		
	}
		
		
	function & getEditorToolbars()
	{
		$path =  CKEDITOR_LIBRARY.DS.'toolbar';
		
		$files = JFolder::files($path);
					
		$toolbars = array();
			
		 foreach($files as $file)
		 {
			
			if(strpos($file,"index") === false && strpos(strrev($file), 'php.') === 0) 
			{
				$toolbars[] = preg_replace('/\.php$/','',$file); 
			}	
			
		 }
		  return $toolbars;
		
	}	
	
	
	function getNextAvailablePluginRowId()
	{
		$db = &JFactory::getDBO();
		
		$db->setQuery('SELECT `row` AS id,count(`row`) AS total FROM `#__jckplugins`'.
					'group by row'.
					'having `row` > 2 ORDER BY `row` DESC LIMIT 1');
		$row = $db->loadObject();
		
		if(!$row && is_null($row))
		{
		 $row = new stdclass;
		 $row->id = 4;
		 $row->order = 1;
		}
		
		$id = $row->id;
		if($row->total = 26)
		  $id++;
		 
		return $id;
	}
	
	
	function getNextLayoutRow($toolbarid)
	{
		$db = &JFactory::getDBO();
		
		$db->setQuery('SELECT `row` AS rowid,MAX(`ordering`) +1  AS rowordering FROM `#__jcktoolbarplugins`'
					.' WHERE `toolbarid`='.(int) $toolbarid
					.' GROUP BY `row`'
					.' ORDER BY `row` DESC LIMIT 1');
		$row = $db->loadObject();
		
		if(!$row && is_null($row))
		{
		 $row = new stdclass;
		 $row->rowid = 4;
		 $row->rowordering = 1;
		}
		
		return $row;
	}
	
	
	
	
}

